$(document).ready(function () {

    // 以JSON方式获取表单数据
    function getJson(form) {
        var map = JSON.parse(JSON.stringify($(form).serializeArray()));
        var result = new Object();
        for(var i = 0; i < map.length; i++) {
            result[map[i].name] = map[i].value;
        }
        return result;
    }
    //条件查询
    $('#search_btn').on('click',function () {
        queryAjax();
    });

    //列表信息显示
    queryAjax();

    function queryAjax() {
        $.ajax({
            type: "POST",
            url: "appInfo/showAppInfo",
            data: getJson($('#search_form')[0]),
            dataType: "json",
            success: function (json) {
                appInfoList(json);
            }
        });
    }

    var $pageNo= $('.info_search input[name=pageNo]');
    var $pageTurning= $('.paging .pageTurning');
    $pageTurning.find(".first").on('click',function () {
        $pageNo.val(parseInt($pageNo.val())-1);
        queryAjax();
    });

    $pageTurning.find(".prev").on('click',function () {
        if( parseInt($pageNo.val()) > 1) {
            $pageNo.val(parseInt($pageNo.val())-1);
            queryAjax();
        }
    });

    $pageTurning.find(".next").on('click',function () {
        if( parseInt($pageNo.val()) < parseInt($('.pageCount').html())) {
            $pageNo.val(parseInt($pageNo.val())+1);
            queryAjax();
        }
    });

    $pageTurning.find(".last").on('click',function () {
        var pageCount= $('.pageCount').text();
        $pageNo.val(parseInt(pageCount));
        queryAjax();
    });

    function appInfoList(json) {
        $('.appInfo_list_body').html("");
        if( json ) {
            $(json.list).each(function () {
                $('.appInfo_list_body').append("<tr>\n" +
                    "                                            <td>"+this.softwareName+"</td>\n" +
                    "                                            <td>"+this.apkName+"</td>\n" +
                    "                                            <td>"+this.softwareSize+"</td>\n" +
                    "                                            <td>"+this.platform+"</td>\n" +
                    "                                            <td>"+this.levelOne+"->"+this.levelTwo+"->"+this.levelThree+"</td>\n" +
                    "                                            <td class='app_state' stateId='"+this.status+"'>"+this.stateName+"</td>\n" +
                    "                                            <td>"+this.downloads+"</td>\n" +
                    "                                            <td>"+this.newVersionName+"</td>\n" +
                    "                                            <td>\n" +
                    "                                                <div class=\"dropdown\">\n" +
                    "                                                    <button type=\"button\" data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"false\" class=\"btn btn-info\">\n" +
                    "                                                        通过审核\n" +
                    "                                                    </button>\n" +
                    "                                                </div>\n" +
                    "                                            </td>\n" +
                    "                                        </tr>");

            });

            $('.dropdown .dropdown-menu li').addClass("dataManipulation");

            // 分页信息
            limitInfo(json);
        }
    }
    function limitInfo(json) {
        $('.paging .totalCount').html(json.totalCount);
        $('.paging .pageCount').html(json.totalPageCount);
        $('.paging .pageNo').html(json.currentPage);
    }
});